package com.cqut.familiarity.service.impl;

import com.cqut.familiarity.entity.Question;
import com.cqut.familiarity.entity.vo.answerVO.AnswerVO;
import com.cqut.familiarity.entity.vo.questionVO.CompletedQuestionVO;
import com.cqut.familiarity.mapper.QuestionMapper;
import com.cqut.familiarity.service.ScoreService;
import com.cqut.familiarity.utils.Cosine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private QuestionMapper questionMapper;


    @Override
    public double[] score(List<AnswerVO> answerVOS){
        double[][] res = new double[2][5];
        // 求和
        for (AnswerVO answerVO : answerVOS){
            String ans = answerVO.getAnswer();
            String questionId = answerVO.getQuestionId();
            Question question = questionMapper.selectById(questionId);
            if (question.getCategory().equals("1")){
                ++res[1][0];
                if (question.getIsReverse().equals("1")){
                    res[0][0] += (4 - Double.valueOf(ans));
                }else {
                    res[0][0] += Double.valueOf(ans);
                }
            }else if(question.getCategory().equals("2")){
                ++res[1][1];
                if (question.getIsReverse().equals("1")){
                    res[0][1] += (4 - Double.valueOf(ans));
                }else {
                    res[0][1] += Double.valueOf(ans);
                }
            }else if(question.getCategory().equals("3")){
                ++res[1][2];
                if (question.getIsReverse().equals("1")){
                    res[0][2] += (4 - Double.valueOf(ans));
                }else {
                    res[0][2] += Double.valueOf(ans);
                }
            }else if(question.getCategory().equals("4")){
                ++res[1][3];
                if (question.getIsReverse().equals("1")){
                    res[0][3] += (4 - Double.valueOf(ans));
                }else {
                    res[0][3] += Double.valueOf(ans);
                }
            }else {
                ++res[1][4];
                if (question.getIsReverse().equals("1")){
                    res[0][4] += (4 - Double.valueOf(ans));
                }else {
                    res[0][4] += Double.valueOf(ans);
                }
            }
        }
        // 取平均
        for (int i = 0; i < 5; ++i){
            if (res[1][i] != 0){
                res[0][i] = res[0][i] / res[1][i];
            }
        }
        res[0][0] = 70 + 10 * (res[0][0] - 1.37) / 0.54;
        res[0][1] = 70 + 10 * (res[0][1] - 1.37) / 0.50;
        res[0][2] = 70 + 10 * (res[0][2] - 1.37) / 0.44;
        res[0][3] = 70 + 10 * (res[0][3] - 1.37) / 0.36;
        res[0][4] = 70 + 10 * (res[0][4] - 1.37) / 0.55;
//        double total = 0;
//        for (double num:res[0]){
//            total += num;
//        }
//        System.out.println(total);
        for (int i = 0; i < 5; ++i){
//            System.out.println(res[0][i]);
//            res[0][i] = (res[0][i] / total) * 100;
//            System.out.println(res[0][i]);
            res[0][i] *= 5.0/6;
        }
        return res[0];
    }

    @Override
    public double similarity(List<CompletedQuestionVO> answer1, List<CompletedQuestionVO> answer2){
//        int size = answer1.size();
        double res = 0;
        for (int i = 0; i < answer1.size() && i < answer2.size(); ++i){
            double ans1 = Double.valueOf(answer1.get(i).getAnswer());
            double ans2 = Double.valueOf(answer2.get(i).getAnswer());
            System.out.println(ans1+"=="+ans2);
            res += Math.abs(ans1 - ans2);
        }

        res = res / answer1.size();

        res = (4 - res )/4 ;
        res *= 100;
        return res;
    }

    @Override
    public double similarity2(List<CompletedQuestionVO> answer1, List<CompletedQuestionVO> answer2){
        int size = answer1.size();
        double res = 0;
        for (int i = 0; i < size; ++i){
            res += Cosine.getSimilarity(answer1.get(i).getAnswer(), answer2.get(i).getAnswer());
        }
        System.out.println(res);
        return (res / size) * 100;
    }


}
